package com.wangjiyuan.giftshuo.ui.fragment.home;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.wangjiyuan.giftshuo.R;
import com.wangjiyuan.giftshuo.adapter.HomeRecyclerAdapter;
import com.wangjiyuan.giftshuo.base.BaseFragment;
import com.wangjiyuan.giftshuo.bean.home.banners.BannersBean;
import com.wangjiyuan.giftshuo.bean.home.column.ItemsBean;
import com.wangjiyuan.giftshuo.bean.home.secondarybanners.SecondaryBannersBean;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class RecyclerFragment extends BaseFragment implements RecyclerContract.RecyclerView {

    private XRecyclerView recyclerView;
    private List<SecondaryBannersBean> secondaryBannersBeanList;
    private List<BannersBean> bannersBeanList;
    private int id;
    private List<ItemsBean> itemsBeanList = new ArrayList<>();
    private HomeRecyclerAdapter adapter;
    private int offset = 20;
    private RecyclerPresenter presenter;

    public RecyclerFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_recycler, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        recyclerView = (XRecyclerView) view.findViewById(R.id.list);

    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        getData();
        Log.e("id", id + "");
        presenter = new RecyclerPresenter(this);
        presenter.loadDateBean(id);
        adapter = new HomeRecyclerAdapter(mContext, itemsBeanList,
                bannersBeanList,
                secondaryBannersBeanList, id);
        LinearLayoutManager layoutManager = new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setAdapter(adapter);
        recyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                presenter.loadDateBean(id);
            }

            @Override
            public void onLoadMore() {
                recyclerView.setRefreshing(false);
                Log.e("list", recyclerView.toString());
            }
        });
        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                switch (newState) {
                    case RecyclerView.SCROLL_STATE_IDLE:
                        presenter.loadNextDateBean(id, offset);
                        break;
                }
            }
        });
    }

    private void getData() {
        Bundle arguments = getArguments();
        bannersBeanList = (List<BannersBean>) arguments.getSerializable("ad");
        secondaryBannersBeanList = (List<SecondaryBannersBean>) arguments.getSerializable("ba");
        id = arguments.getInt("id");
    }

    @Override
    public void loadDate(List<ItemsBean> list) {
        itemsBeanList.clear();
        itemsBeanList.addAll(0, list);
        recyclerView.refreshComplete();
        adapter.notifyDataSetChanged();
    }

    @Override
    public void loadNextDate(List<ItemsBean> list) {
        int size = itemsBeanList.size();
        itemsBeanList.addAll(size - 1, list);
        adapter.notifyItemInserted(size);
        offset += 20;
    }


}
